Okay.
So in today's lesson we're going to finish off functions.
Okay.
So I know there's been quite a lot of lessons on functions.
So we're going to be finishing all those off in this lesson okay.
We're going to look at how what we're turning means when it comes to functions and how to do it okay.
So for this one I'm going to add in a folder and add in a script into server script service.
Okay.
So what actually is returning.
Well when you run a function or when when you write out a function, sometimes you want the function
to give out a value once it's finished.
So this could be a boolean saying that the function worked.
It could be that your function is.
The purpose of your function is to find a certain value, and it gives out that value at the end.
Okay.
And the process, the process of giving out that value is called returning.
Okay.
I'll give you an example.
So let's create a function and let's call this um I don't know um function.
Uh, um, let's add a part time.
And let's say is transparent.
Okay.
So what this function is going to do is going to check to see if the parts transparency is one.
So it's always reference the part as well.
Okay.
So um, we're going to say if part dot transparency is equal to equal to one, then we obviously want
to let it we want to um put out or return that to be transparent that is transparent.
And then else if it's not we do some other stuff.
Okay.
So how do we return things.
Well it's actually very simple.
Okay.
You just write the keyword return like that, and then you write to a bit of data that you want to return.
So this could be a boolean.
It could be a number.
It could be any any data you like okay.
But in this case I'm going to return true okay.
Because if it is transparent um, I want it to return true and else I want to return or return false.
Okay.
So what's the point of this?
Well, later on in your code, um, let's say you want to change a property, but only if the part is
not transparent.
Okay?
Instead of checking it yourself, you could use the.
You could use the function to check it for you.
So you could then say, um, if is transparent then um, sorry.
If not is transparent.
So if, um, transparent is transparent is equal to false, then we can print whether the part is visible.
And you can then change a property of it.
Let's say the material is equal to enum dot material dot I don't know grass for example.
Okay.
So that's the point of this is to get your function not only to do something, but to output a value
once it's finished.
And this is very useful like in this example here, when we're checking something okay.
Now obviously in, you know proper examples, we're going to have a look at in a minute.
It's not going to be as easy as just a one line, because otherwise you could just check it down here.
But you can have you can have a bit more complicated script that for example, might check to see if
one player is, um, has a line of line of sight to another to another place.
If one player can see another player, that would be a lot more complicated to script and just one line,
okay, which makes it a lot easier to put it inside of a function.
So when you need to use it later on, you can very easily just, um, put in the name of the function
rather than writing all that code out.
So all it is, it just saves you time and makes your code a lot more easier to understand.
Okay, because any big blocks of code, you can hide them away in functions so that you can just use
them when you need to and not have to copy and paste them within your code.
So let's have a look at another example.
Okay, so what I'd like to do is um, I would like to check to see if this folder has parts inside of
it.
Okay.
So I'm going to create a function local function called has parts.
Um yeah.
Has parts okay.
We're going to check to see if this folder has any parts inside of it.
So we're going to say um game dot workspace dot folder okay.
And um let's let's go with this.
So um, let's first off figure out how we're going to check if the folder has parts okay.
So sorry local folder not local part.
So to check if it has parts we would use the find first child okay.
So folder colon find first child child.
How.
Oh I've written that completely wrong.
Now you can see there's actually some other types of define first child function okay.
Now find first child will use a the name of a child.
However, a part could technically have any name.
So instead we can use the which is a um.
So instead of saying just find first child.
Instead we can use find first child, which is a okay, they do get quite long some of your functions.
And what this will check is if it has a child which is of a certain class.
The class we're looking at is being a part.
Okay.
So in speech marks we can put part like that there.
So it's not going to check if any anything has been named part.
But instead just check that it has a class of a part.
Okay.
So it's a bit of code.
Now um, if this here is nil, it means there's no parts.
If it's not nil, it means there are parts.
So we can check if that's the case.
Okay.
So if it is the case then the folder does have parts.
So we want to return true okay.
Else.
So if the folder does not have parts we can then return false okay.
And then later on in our script we could maybe say um if has parts then print folder has parts as children
okay.
So hopefully you can kind of see the use of returning okay.
It makes your functions a lot more useful as in some cases you are allowed you can return stuff instead
of just getting a function to do something, it actually gives you something back in return to use.
Okay, um, and there you go.
That is all for functions.
Okay.
Obviously we functions are very important and we'll be using them a lot.
But that's all you really need to know about functions okay.
Um, so make sure you try all these out.
Make sure you're okay with all these lessons and lectures.
Okay.
If you need to go back at some, that's completely fine.
Okay.
Um, I just personally would not try not to move on in the course until you are fairly confident with
these, as we'll be using these quite a lot later on, especially in the next scripting section as well.
So they're a bit difficult.
And again, they might take a bit longer to learn on other things.
Some things take longer to learn than others.
But um, once you are okay with these or you're fairly confident with them, done some practicing,
then you can join me in the next lecture where we'll continue on learning about some more different
topics.
But you hopefully you already don't know.